package com.itheima.day15.TCP.test9;

import sun.nio.ch.ThreadPool;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import java.util.concurrent.*;

/**
 * 聊天室Demo(BIO)
 * 多个客户端发送消息给服务端后服务端转发给所有客户端
 *
 */
public class ChatServerDemo {
    public static ArrayList<Socket> SocketList = new ArrayList<>();
    private static String msg = null;

    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(10002);
        Scanner sc = new Scanner(System.in);

        //创建线程池
        //ExecutorService executorService = Executors.newFixedThreadPool(200);
        //更改用参数创建线程池方式
        ThreadPoolExecutor pool = new ThreadPoolExecutor(
                1,//核心线程数量
                2,//线程池的总数量
                10,//临时线程空闲时间
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(1),//阻塞队列
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());//拒绝策略
        while (true) {
            //开启监听
            System.out.println("开启监听");
            Socket accept = ss.accept();
            if (accept != null) {
                //将监听到的客户端放入集合
                SocketList.add(accept);
                System.out.println("监听到" + accept.getPort());
            }
            pool.submit(() -> {
                while (true) {
                    try {
                        //网络中的流
                        BufferedReader isr = new BufferedReader(new InputStreamReader(accept.getInputStream()));

                        if ((msg = isr.readLine()) != null) {
                            System.out.println("服务端接收:" + msg);
                            synchronized (msg) {
                                //创建输出流
                                BufferedWriter osr = null;
                                //将接收到的数据发送给所有连接的客户端
                                Iterator<Socket> iterator = SocketList.iterator();
                                while (iterator.hasNext()) {
                                    Socket next = iterator.next();
                                    if (accept != next) {
                                        osr = new BufferedWriter(new OutputStreamWriter(next.getOutputStream()));
                                        osr.write(msg);
                                        osr.newLine();
                                        osr.flush();
                                    }
                                }
                                msg = null;
                            }
                        }


                        //System.out.println("接受成功");
                    } catch (IOException e) {
                        System.out.println("客户端" + accept.getPort() + "连接中断");
                        SocketList.remove(accept);
                        break;
                    } finally {

                    }
                }
            });


        }


    }

}




